package org.edu.star.spi;

import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;

/**
 * @author xinxing
 * @date 2023/2/6 17:37
 */
public class LoggerService {

    private static final LoggerService loggerService = new LoggerService();

    private final Logger logger;

    private final List<Logger> loggers;

    private LoggerService() {
        ServiceLoader<Logger> load = ServiceLoader.load(Logger.class);

        List<Logger> list = new ArrayList<>();

        for (Logger logger : load) {
            list.add(logger);
        }

        loggers = list;

        if (!loggers.isEmpty()) {
            logger = list.get(0);
        } else {
            logger = null;
        }
    }

    public static LoggerService getInstance() {
        return loggerService;
    }

    public void info(String message) {
        if (logger != null) {
            logger.info(message);
        } else {
            System.out.println("logger不存在");
        }
    }

    public void error(String message) {
        if (logger != null) {
            logger.error(message);
        } else {
            System.out.println("logger不存在");
        }
    }
}
